package cn.jhc.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.jhc.bean.ShopBean;
import cn.jhc.bean.UserBean;
import cn.jhc.dao.ShopDAO;

@WebServlet(name = "Admin Check list", urlPatterns = { "/adminchecklist.do" })
public class AdminChecklistServlet extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		List<ShopBean> list = new ArrayList<ShopBean>();
		UserBean adminuser = (UserBean)session.getAttribute("adminuser");
		String years = req.getParameter("years");
		String month = req.getParameter("month");
		String th = req.getParameter("th");
		String datetime = years + "-" + month + "-" + th;
		ShopDAO dao = null;
		if (null != adminuser) {
			dao = new ShopDAO();
			try {
				list = dao.adminChecklist(datetime);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (list.size() == 0) {
				String a="a";
				req.setAttribute("a", a);
				RequestDispatcher dispatcher = req
						.getRequestDispatcher("/years.jsp");
				dispatcher.forward(req, resp);
				return;
			} else {
				req.setAttribute("list", list);
				RequestDispatcher dispatcher = req
						.getRequestDispatcher("/adminchecklist.jsp");
				dispatcher.forward(req, resp);
				return;
			}
		} else {
			String getb = (String) req.getParameter("getb");
			req.setAttribute("getb", getb);
			RequestDispatcher dispatcher = req
					.getRequestDispatcher("/login.jsp");
			dispatcher.forward(req, resp);
			return;
		}
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
}
